<?
require("../../common/init.php");
require("author.php");
$shipment_method=new DB(Tshipment_method, $conn);

$title="物流价格";
$gee=$shipment_cost;  // 物流费用  shipment fee
$cate=$shipment_method; // 物流方式  shipment method
$cateid=0;

$gee->rows=15;
$url='shipment_cost';

$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除 
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD 
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"];  // 页码 page
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);
$zoneid=0; if(isset($_GET["zoneid"]))if(is_numeric($_GET["zoneid"]))$zoneid=$_GET["zoneid"]; // 地区id zoneid
if($zoneid==0) $zoneid=44;
$zone=$country;
$zonetype='country'; if (isset($_GET["zonetype"])) $zonetype = clean($_GET["zonetype"]); // 地区类型: 国家/省份, zone type
if($zonetype=='province') $zone=$province;

// sql action / sql 动作
switch ($action){
	case "update":
	sql_update();
	break;
	case "add":
	sql_insert();
	break;
}

if($id==0){
	$order='seq_no';
	$where=array();
	$info=$cate->get_rows_advanced(32,$where,$order);
}
else $info=$gee->get_row($id);

function add()
{
	global $url,$h;
	$row = array();
	$h->form($url.'.php', 0, '');
	show_box($row, true, ture);		
	$h->hidden('action', 'add');
	$h->former();
}
function update($info)
{
	global $gee, $url, $h, $id;
	if($id==0) 
	{
		echo "Invali argument!!!";
		exit;
	}
	echo $h->form($url.'.php', 0, '');
  	show_box($info, true, false);
	echo $h->hidden('action', 'update');
	echo $h->former();
}
function delete($id)
{
	global $gee,$url,$zonetype,$zoneid;
	if($gee->delete($id))
	wheel($url.'.php?zonetype='.$zonetype.'&zoneid='.$zoneid);
}	
function show_info($info)
{
	global $gee, $id;
	if($id==0)
	{
  	show_boxes($info);
	}
	else 
	{
		show_box($info, false);
	}
}
function sql_insert()
{
	global $gee,$url;
	$zonetype=$_POST["zonetype"];
	$zoneid=$_POST[$zonetype.'_id'];
	$arr=array(
		$zonetype.'_id'=>$zoneid,
		'weight'=>iso(@$_POST["weight"]),
		'cost'=>iso(@$_POST["cost"]),
		'shipment_method_id'=>$_POST['shipment_method_id'],
	);
	//pre($arr); exit;
	if($gee->insert($arr))
	wheel($url.'.php?zonetype='.$zonetype.'&zoneid='.$zoneid);
}
function sql_update()
{
	global $gee,$url;
	$zonetype=$_POST["zonetype"];
	$zoneid=$_POST[$zonetype.'_id'];
	$shipment_method_id=$_POST['shipment_method_id'];

	$where=array();
	$where[]=array('name'=>$zonetype.'_id', 'val'=>$zoneid);
	$where[]=array('name'=>'shipment_method_id', 'val'=>$shipment_method_id);
	$tem=$gee->get_rows_advanced(1,$where);

	if(!count($tem)){ // 不存在, 新增之
		$arr=array(
			$zonetype.'_id'=>$zoneid,
			'weight'=>iso(@$_POST["weight"]),
			'cost'=>iso(@$_POST["cost"]),
			'weight_margin'=>iso(@$_POST["weight_margin"]),
			'cost_margin'=>iso(@$_POST["cost_margin"]),
			'shipment_method_id'=>$_POST['shipment_method_id'],
		);
		if($gee->insert($arr))
		wheel($url.'.php?zonetype='.$zonetype.'&zoneid='.$zoneid);
	}
	else{ // 存在, 更新之
		$tem=$tem[0];
		$arr=array(
			//$zonetype.'_id'=>$zoneid,
			'weight'=>iso(@$_POST["weight"]),
			'cost'=>iso(@$_POST["cost"]),
			'weight_margin'=>iso(@$_POST["weight_margin"]),
			'cost_margin'=>iso(@$_POST["cost_margin"]),
			//'shipment_method_id'=>$_POST['shipment_method_id'],
		);
		if($gee->update($arr,'id',$tem['id']))
		wheel($url.'.php?zonetype='.$zonetype.'&zoneid='.$zoneid);
	}
}

// view 加载视图
require ROOTVIEW.$url.'.php';

// end of shipemnt_cost